package cn.xiongxf.product.service.impl;

import cn.xiongxf.basic.exception.BusinessException;
import cn.xiongxf.basic.service.IBaiduAiAuditService;
import cn.xiongxf.basic.service.impl.BaseServiceImpl;
import cn.xiongxf.basic.util.LoginContext;
import cn.xiongxf.org.domain.Employee;
import cn.xiongxf.org.mapper.EmployeeMapper;
import cn.xiongxf.product.domain.Product;
import cn.xiongxf.product.domain.ProductDetail;
import cn.xiongxf.product.domain.ProductOnlineAuditLog;
import cn.xiongxf.product.mapper.ProductDetailMapper;
import cn.xiongxf.product.mapper.ProductMapper;
import cn.xiongxf.product.mapper.ProductOnlineAuditLogMapper;
import cn.xiongxf.product.service.IProductService;
import cn.xiongxf.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Transactional
    @Override
    public void add(Product product) {
        super.add(product);
        // 获取服务详情
        ProductDetail productDetail = product.getProductDetail();
        if(productDetail != null){
            productDetail.setProductId(product.getId());
            // 添加服务详情
            productDetailMapper.save(productDetail);
        }
    }

    @Transactional
    @Override
    public void update(Product product) {
        super.update(product);
        if(product.getProductDetail() != null){
            ProductDetail productDetail = product.getProductDetail();
            productDetail.setProductId(product.getId());
            // 服务详情更新：根据服务详情id去更新
            productDetailMapper.update(productDetail);
        }
    }

    @Transactional
    @Override
    public void del(Long id) {
        super.del(id);
        productDetailMapper.removeProductDetailByProductId(id);
    }

    @Transactional
    @Override
    public void onsale(List<Long> ids, HttpServletRequest request) {
        for (Long id : ids) {
            Product product = productMapper.loadById(id);
            // 1.审核
            // 审核文本
            Boolean textBoo = baiduAiAuditService.textAudit(product.getName());
            //Boolean textBoo = BaiduAiUtils.textCensor(product.getName());
            // 审核图片
            Boolean imageBoo = baiduAiAuditService.imageAudit(product.getResources());

            if(textBoo && imageBoo){  // 审核成功
                // a.设置上架时间
                product.setOnsaletime(new Date());
                // 修改状态：将状态由0 - 1
                product.setState(1L);
                productMapper.update(product);

                // b.添加上架审核日志 - 审核成功的日志
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setProductId(id);
                log.setState(1);
                Logininfo logininfo = LoginContext.getLogininfo(request);
                Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
                log.setAuditId(employee.getId());
                log.setAuditTime(new Date());
                log.setNote("审核成功");
                productOnlineAuditLogMapper.save(log);

            }else{  // 审核失败
                // 添加上架审核日志 - 审核失败的日志
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setProductId(id);
                log.setState(0);
                Logininfo logininfo = LoginContext.getLogininfo(request);
                Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
                log.setAuditId(employee.getId());
                log.setNote("审核失败");
                productOnlineAuditLogMapper.save(log);
            }
        }
    }

    @Transactional
    @Override
    // 加if判断：如果已上/下架，不操作数据库，选全下架再下架返回提示
    public void offsale(List<Long> ids) {
        // 修改状态：将状态由1 - 0
        productMapper.offsale(ids, new Date());
    }

}
